package com.lingtianyu.tech.erbaoshen.core.consumer.bean;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class ReferenceBean<T> implements FactoryBean<T> {
    private String interfaceName;
    private Class<?> interfaceClass;
    private String group;
    private String version;

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public T getObject() throws Exception {
        ReferenceBeanProxy referenceBeanProxy = new ReferenceBeanProxy(getInterfaceName(), getInterfaceClass(), getGroup(), getVersion());
        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, referenceBeanProxy);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }
}
